package com.ticket.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ticket.system.domain.Yanchu;
import com.ticket.system.mapper.YanchuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ticket.system.mapper.YanchuAttachMapper;
import com.ticket.system.domain.YanchuAttach;
import com.ticket.system.service.IYanchuAttachService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 演出附表Service业务层处理
 * 
 * @author gjchen
 * @date 2023-04-03
 */
@Service
public class YanchuAttachServiceImpl implements IYanchuAttachService 
{
    @Autowired
    private YanchuAttachMapper yanchuAttachMapper;

    @Autowired
    private YanchuMapper yanchuMapper;

    /**
     * 查询演出附表
     * 
     * @param id 演出附表主键
     * @return 演出附表
     */
    @Override
    public YanchuAttach selectYanchuAttachById(Long id)
    {
        return yanchuAttachMapper.selectYanchuAttachById(id);
    }

    /**
     * 查询演出附表列表
     * 
     * @param yanchuAttach 演出附表
     * @return 演出附表
     */
    @Override
    public List<YanchuAttach> selectYanchuAttachList(YanchuAttach yanchuAttach)
    {
        return yanchuAttachMapper.selectYanchuAttachList(yanchuAttach);
    }
    @Override
    public YanchuAttach selectYanchuAttachCollect(YanchuAttach yanchuAttach){
        return yanchuAttachMapper.selectYanchuAttachCollect(yanchuAttach);
    }

    /**
     * 新增演出附表
     * 
     * @param yanchuAttach 演出附表
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertYanchuAttach(YanchuAttach yanchuAttach)
    {
        if(yanchuAttach.getNumTicket()<0){
            throw new RuntimeException("数量必须大于0");
        }
        if(null!=yanchuAttach.getPriceTicket() && yanchuAttach.getPriceTicket().doubleValue()<0){
            throw new RuntimeException("票价必须大于0");
        }
        int count = yanchuAttachMapper.insertYanchuAttach(yanchuAttach);
        if(count<=0){
            return 0;
        }
        YanchuAttach yanchuAttachTotal = yanchuAttachMapper.selectAttachTotal(yanchuAttach.getIdYanchu().longValue());
        if(null==yanchuAttachTotal){
            return 0;
        }
        Yanchu yanchu = new Yanchu();
        yanchu.setId(yanchuAttach.getIdYanchu().longValue());
        yanchu.setChengbenLaowu(yanchuAttachTotal.getTotalBuzhu());
        yanchu.setRenshuLaowu(yanchuAttachTotal.getNumBuzhu());
        yanchu.setJinePiaowu(yanchuAttachTotal.getShishouTicket());
        yanchu.setShuliangPiaowu(yanchuAttachTotal.getNumTicket());
        yanchu.setShouruYansheng(yanchuAttachTotal.getJineYansheng());
        yanchuMapper.updateYanchu(yanchu);
        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBatchYanchuAttach(List<YanchuAttach> yanchuAttachs)
    {
        int count = yanchuAttachMapper.insertBatchYanchuAttach(yanchuAttachs);
        if(count==0){
            return count;
        }
        YanchuAttach yanchuAttachTotal = yanchuAttachMapper.selectAttachTotal(yanchuAttachs.get(0).getIdYanchu().longValue());
        if(null == yanchuAttachTotal){
            return count;
        }
        Yanchu yanchu = new Yanchu();
        yanchu.setId(yanchuAttachs.get(0).getIdYanchu().longValue());
        yanchu.setChengbenLaowu(yanchuAttachTotal.getTotalBuzhu());
        yanchu.setRenshuLaowu(yanchuAttachTotal.getNumBuzhu());
        yanchu.setJinePiaowu(yanchuAttachTotal.getShishouTicket());
        yanchu.setShuliangPiaowu(yanchuAttachTotal.getNumTicket());
        yanchu.setShouruYansheng(yanchuAttachTotal.getJineYansheng());
        yanchuMapper.updateYanchu(yanchu);
        return count;
    }

    /**
     * 修改演出附表
     * 
     * @param yanchuAttach 演出附表
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateYanchuAttach(YanchuAttach yanchuAttach)
    {
        int count = yanchuAttachMapper.updateYanchuAttach(yanchuAttach);
        if(count<=0){
            return 0;
        }
        YanchuAttach yanchuAttachTotal = yanchuAttachMapper.selectAttachTotal(yanchuAttach.getIdYanchu().longValue());
        if(null==yanchuAttachTotal){
            return 0;
        }
        Yanchu yanchu = new Yanchu();
        yanchu.setId(yanchuAttach.getIdYanchu().longValue());
        yanchu.setChengbenLaowu(yanchuAttachTotal.getTotalBuzhu());
        yanchu.setRenshuLaowu(yanchuAttachTotal.getNumBuzhu());
        yanchu.setJinePiaowu(yanchuAttachTotal.getShishouTicket());
        yanchu.setShuliangPiaowu(yanchuAttachTotal.getNumTicket());
        yanchu.setShouruYansheng(yanchuAttachTotal.getJineYansheng());
        yanchuMapper.updateYanchu(yanchu);
        return count;
    }

    /**
     * 批量删除演出附表
     * 
     * @param ids 需要删除的演出附表主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteYanchuAttachByIds(Long[] ids)
    {
        if(null==ids || ids.length==0){
            return 0;
        }
        int count = 0;
        for(Long id:ids){
            count+=deleteYanchuAttachById(id);
        }

        return count;
    }

    /**
     * 删除演出附表信息
     * 
     * @param id 演出附表主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteYanchuAttachById(Long id)
    {
        YanchuAttach attach = this.selectYanchuAttachById(id);
        if(null==attach){
            return 0;
        }
        int count = yanchuAttachMapper.deleteYanchuAttachById(id);
        if(count<=0){
            return 0;
        }
        YanchuAttach yanchuAttachTotal = yanchuAttachMapper.selectAttachTotal(attach.getIdYanchu().longValue());
        Yanchu yanchu = new Yanchu();
        yanchu.setId(attach.getIdYanchu().longValue());
        if(null==yanchuAttachTotal){
            yanchu.setChengbenLaowu(BigDecimal.ZERO);
            yanchu.setRenshuLaowu(0L);
            yanchu.setJinePiaowu(BigDecimal.ZERO);
            yanchu.setShuliangPiaowu(0);
            yanchu.setShouruYansheng(BigDecimal.ZERO);
        }else{
            yanchu.setChengbenLaowu(yanchuAttachTotal.getTotalBuzhu());
            yanchu.setRenshuLaowu(yanchuAttachTotal.getNumBuzhu());
            yanchu.setJinePiaowu(yanchuAttachTotal.getShishouTicket());
            yanchu.setShuliangPiaowu(yanchuAttachTotal.getNumTicket());
            yanchu.setShouruYansheng(yanchuAttachTotal.getJineYansheng());
        }
        yanchuMapper.updateYanchu(yanchu);
        return count;
    }
}
